<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/19
 * Time: 10:46
 */

namespace app\zucai\home;

use app\index\controller\Home;
use think\Db;

/**
 * Class Account
 * @package app\zucai\home
 * 用户个人信息查询
 */
class Account extends Home
{
    //用户个人信息接口
    public function message(){//传参为 用户id
        if($this->request->isPost()){
            if($this->request->post()){
                $post=$this->request->post();

                if (!(isset($post['id']) && $post['id'])) return returnError('未获取到id');

                $con = Db::name('user')
                    ->field('id,user,name,paypassword,mobile,identity,avatar,money,content,status,create_time,last_login_time')
                    ->where('id', $post['id'])
                    ->find();
                if(in_array($con['avatar'],array(1,2,3,4,5,6,7,8,9,10,11))){
                    $con['avatar']= get_file_path($con['avatar']);
                }
                if($con['paypassword']==null){
                    $con['pay']=0;
                }else{
                    $con['pay']=1;
                }
                unset($con['paypassword']);
                if($con['status']==0){
                    return returnError('该用户已被禁用！');
                }else{
                $data_list = Db::name('order')
                    ->where('isdraw', 2)
                    ->where('uid', $post['id'])
                    ->select();
                $allmoney = 0;
                foreach ( $data_list as $k => $v) {
                    $allmoney += $v['mode'];
                }
                $cont=Db::name('order')
                    ->where('isdraw', 'in','2,3')
                    ->where('uid', $post['id'])
                    ->count();
                $con['allplay']=$cont;     //总投注次数
                $con['allmoney']=$allmoney; //总获奖金额
                $con['zhong']=count($data_list); //总中奖次数

                //7天内的投注情况
                $data=Db::name('order')
                    ->field('id,isdraw,oddtime')
                    ->order('oddtime')
                    ->where('isdraw', 'in','2,3')
                    ->where('oddtime', '>=',date('Y-m-d', strtotime('-7 days')))
                    ->where('uid', $post['id'])
                    ->select();
                $data2=Db::name('order')
                    ->where('isdraw', 2)
                    ->where('oddtime', '>=',date('Y-m-d', strtotime('-7 days')))
                    ->where('uid', $post['id'])
                    ->count();
                //计算7天内连中次数
                $flag=0;$max=0;
                for($i=0;$i<count($data);$i++){
                    if($data[$i]['isdraw']==2){
                        $flag++;
                        if($flag>$max){
                            $max=$flag;
                        }
                    }else{
                        $flag=0;
                    }
                }
                $con['timeplay']=count($data);     //7天内总投注次数
                $con['timenumber']=$data2; //7天内总中奖次数
                $con['target']=$max; //7天内连中次数
                if($con){
                        return returnOk($con);
                }else{
                    return returnError('暂无数据！');
                }
                }
            }else{
                return returnError('post传值错误！');
            }
        }else{
            return returnError('请使用POST方式请求');
        }
    }
    //用户账户资金明细接口
    public function index(){ //传参为 用户id
        if($this->request->isPost()){
            if($this->request->post()){
                $post=$this->request->post();
				
                $con = Db::name('account')
                    ->field('type,money,lastmoney,dmoney,time')
                    ->where('uid', $post['id'])
                    ->where('type','in','1,2,3,4')
                    ->order('id DESC')
                    ->select();
				foreach ($con as $key=>$value){
					$con[$key]['money']=round($con[$key]['lastmoney']-$con[$key]['money'],2);
				}
                if($con){
                    return returnOk($con);
                }else{
                    return returnError('暂无数据！');
                }
            }else{
                return returnError('post传值错误！');
            }
        }else{
            return returnError('请使用POST方式请求');
        }
    }

    //用户账户资金冻结明细接口
    public function dong(){ //传参为 用户id
        if($this->request->isPost()){
            if($this->request->post()){
                $post=$this->request->post();
                $con = Db::name('account')
                    ->where('uid', $post['id'])
                    ->where('type','in','5,6')
                    ->order('id DESC')
                    ->select();
                if($con){
                    return returnOk($con);
                }else{
                    return returnError('暂无数据！');
                }
            }else{
                return returnError('post传值错误！');
            }
        }else{
            return returnError('请使用POST方式请求');
        }
    }

    //用户购彩实战记录
    public function fight(){ //传参为 用户id
        if($this->request->isPost()){
            if($this->request->post()){
                $post=$this->request->post();
                $con = Db::name('order')
                    ->field('id,uid,isdraw,itemcount,okamount,amount,type,oddtime')
                    ->where('uid', $post['id'])
                    ->where('isdraw','in','2,3')
                    ->order('oddtime DESC')
                    ->select();
                if($con){
                    return returnOk($con);
                }else{
                    return returnError('暂无数据！');
                }
            }else{
                return returnError('post传值错误！');
            }
        }else{
            return returnError('请使用POST方式请求');
        }
    }
    //用户购彩保存方案接口
    public function keep(){ //传参为 用户id
        if($this->request->isPost()){
            if($this->request->post()){
                $post=$this->request->post();
                $con = Db::name('order')
                    ->field('id,uid,trano,isdraw,itemcount,okamount,type,oddtime')
                    ->where('uid', $post['id'])
                    ->where('isdraw','in','0,1,4,5')
                    ->order('oddtime DESC')
                    ->select();
                if($con){
                    return returnOk($con);
                }else{
                    return returnError('暂无数据！');
                }
            }else{
                return returnError('post传值错误！');
            }
        }else{
            return returnError('请使用POST方式请求');
        }
    }
	
	//跟单公告
	    public function tiao(){
				 if($this->request->isPost()){
				if($this->request->post()){
					$post=$this->request->post();
		   $con = Db::name('admin_user')->field('id')
				->where('sign',$post['sign'])			
				->find();
			$cont = Db::name('admin_order')->field('pid,uid,mode')
				->where('isdraw', 2)
				->where('type', 1)
				->where('aid',$con['id'])
				->select();
			foreach ($cont as $k=>$v){
				$c=Db::name('user')->field('name')->where('id',$cont[$k]['pid'])->find();
				$cont[$k]['pid']=$c['name'];
				$c=Db::name('user')->field('name')->where('id',$cont[$k]['uid'])->find();
				$cont[$k]['uid']=$c['name'];
			}
			if($cont){
				return returnOk($cont);
			}else{
				return returnError('暂无数据');
			}
			}else{
					return returnError('post传值错误！');
				}
			}else{
				return returnError('请使用POST方式请求');
			}
    }

}